﻿using Acme.BookStore.Authors;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;

namespace Acme.BookStore.Bass
{
    public class BasAppService : BookStoreAppService, IBasAppService
    {
        private readonly IBasRepository _basRepository;
        private readonly BasManager _basManager;

        private readonly IAuthorRepository _authorRepository;
        private readonly AuthorManager _authorManager;

        private readonly ISqlSugarRepository _sqlSugarRepository; // 仓储对象：封装简单的CRUD
        private readonly SqlSugarScope db; // 核心对象：拥有完整的SqlSugar全部功能 单例

        public BasAppService(IBasRepository basRepository, BasManager basManager, IAuthorRepository authorRepository, AuthorManager authorManager, ISqlSugarRepository sqlSugarRepository, SqlSugarScope Db)
        {
            _authorRepository = authorRepository;
            _authorManager = authorManager;
            _basRepository = basRepository;
            _basManager = basManager;
            _sqlSugarRepository = sqlSugarRepository;
            db = Db;
        }

        [IgnoreAntiforgeryToken]
        public async Task<List<BasDto>> GetListAsync()
        {
            var ce = await _basRepository.ToListAsync();

            return ObjectMapper.Map<List<Bas>, List<BasDto>>(ce);
        }

        /// <summary>
        /// 测试工作单元（uow 成功）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [IgnoreAntiforgeryToken]
        [SqlSugarUnitOfWork]
        public async Task<BasDto> CreateBasAsync(BasDto input)
        {
            var author = await _authorManager.CreateAsync(
              input.Name,
               DateTime.Today,
              input.Code
          );

            await _authorRepository.InsertAsync(author);

            var bas = new Bas() { Code = input.Code };
            var entity = await _basRepository.InsertAsync(bas);

            if (input.Name == "e")
            {
                throw new BusinessException("ee");
            }
            return ObjectMapper.Map<Bas, BasDto>(entity);
        }

        /// <summary>
        /// 测试工作单元furion uow成功
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [IgnoreAntiforgeryToken]
        [SqlSugarUnitOfWork]//添加SqlSugarUnitOfWork特性启用sqlsugar事务
        public async Task<BasDto> CreateBasByFurionAsync(BasDto input)
        {
            var author = await _authorManager.CreateAsync(
              input.Name,
               DateTime.Today,
              input.Code);

            await _authorRepository.InsertAsync(author);

            var bas = new Bas() { Code = input.Code };
            var entity = await db.Insertable(bas).ExecuteReturnEntityAsync();
            var basRepository = _sqlSugarRepository.Change<Bas>();
            entity.Code = input.Code + "new";
            await basRepository.UpdateAsync(entity);
            if (input.Name == "e")
            {
                throw new BusinessException("ee");
            }
            return ObjectMapper.Map<Bas, BasDto>(entity);
        }

        /// <summary>
        /// 测试工作单元Manager(成功)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [IgnoreAntiforgeryToken]
        [SqlSugarUnitOfWork]
        public async Task<BasDto> CreateBasByManagerAsync(BasDto input)
        {
            var author = await _authorManager.CreateAsync(
              input.Name,
               DateTime.Today,
              input.Code);

            await _authorRepository.InsertAsync(author);

            var entity = await _basManager.CreateAsync(input.Code, input.Name);
            await _basRepository.InsertAsync(entity);
            var basRepository = _sqlSugarRepository.Change<Bas>();
            entity.Name = input.Name + "new";
            await basRepository.UpdateAsync(entity);
            if (input.Name == "e")
            {
                throw new BusinessException("ee");
            }
            return ObjectMapper.Map<Bas, BasDto>(entity);
        }
    }
}